package inits

import (
	"coroutine/api_client/basic/config"
	"github.com/google/uuid"
	"github.com/hashicorp/consul/api"
	"github.com/spf13/viper"
	"log"
)

func init() {
	InitConfig()
}
func InitConfig() {
	viper.SetConfigFile("D:\\goWork\\src\\coroutine\\api_client\\dev.yaml")
	err := viper.ReadInConfig()
	if err != nil {
		panic("viper配置读取失败")
	}
	log.Println("viper配置读取成功")
	err = viper.Unmarshal(&config.AppConf)
	if err != nil {
		panic("viper配置解析失败")
	}
	log.Println("viper配置解析成功", config.AppConf)

	AppConf := config.AppConf.System

	defaultConfig := api.DefaultConfig()

	defaultConfig.Address = "14.103.243.15"
	client, _ := api.NewClient(defaultConfig)
	a := new(api.AgentServiceRegistration)
	a.Address = AppConf.Host
	a.Port = AppConf.Port
	IdCard := uuid.NewString()
	a.ID = IdCard
	a.Name = AppConf.Name
	a.Tags = []string{"coroutine", "api_client"}
	client.Agent().ServiceRegister(a)
}
